import axios from 'axios'
import context from '../main.js'
import {
	Loading
} from "element-ui";
import {
	MessageBox
} from "element-ui";

// http request拦截器 添加一个请求拦截器
axios.interceptors.request.use(function(config) {
	// Do something before request is sent
	let token = context.$store.state.user.userinfo.token
	if (token == undefined || token == "undefined") {
		token = "ssss";
	}
	if (token) {
		config.headers.accessToken = token; //将token放到请求头发送给服务器
		return config;
		//这里经常搭配token使用，将token值配置到tokenkey中，将tokenkey放在请求头中
		// config.headers['accessToken'] = Token;
	}
})
// 响应拦截
axios.interceptors.response.use(res => {
	if (res.data.code == 200) {
		return res
	} else if (res.data.code == 401) {
		// MessageBox.alert("token验证失败");W
		goBackHome();
	} else if (res.data.code == 400) {
		console.log(222222222222)
		MessageBox.alert(res.data.message);
	}else if (res.data.code == 1001) {
		MessageBox.alert("终端ip认证失败" + res.data.message);
		goBackHome(); 
	} else if (res.data.code == 1005) {
		return Promise.reject(res);
	} else {
		// if(!res.data.message == "GetPatInfo方法出错:无此身份证号患者的就诊信息!"){
		MessageBox.alert("错误:" + (res.data.message), '温馨提示', {
			confirmButtonText: "确定",
			callback: action => {
				MessageBox.close();
				setTimeout(() => {
					MessageBox.close();
					context.$store.dispatch("user/updateuserinfo", "");
					context.$store.dispatch("appointment/updateAppointmentInfo", "");
					context.$store.dispatch("payrecord/updatepayinfo", "");
					context.$store.dispatch("recipel/updateinvoiceNo", "");
					context.$store.dispatch("recipel/updateRecordsInfo", "");
					context.$store.dispatch("recipel/updateBillInfo", "");
					goBackHome();
				}, 500);
			}
		});
		// }
		return Promise.reject(res);
	}
}) 

// 检查http状态码返回是否正常
function checkStatus(response) {
	console.log('response',response)
	// 如果http状态码正常，则直接返回数据
	if (response && (response.status === 200 || response.status === 304 ||
			response.status === 400)) {
		return response;
	}
    let message = '网络异常,请稍后重试!或者联系管理员.';
	MessageBox.alert(message, {
		confirmButtonText: "确定",
		callback: action => {
			MessageBox.close();
			setTimeout(() => {
				MessageBox.close();
				context.$store.dispatch("user/updateuserinfo", "");
				context.$store.dispatch("appointment/updateAppointmentInfo", "");
				context.$store.dispatch("payrecord/updatepayinfo", "");
				context.$store.dispatch("recipel/updateinvoiceNo", "");
				context.$store.dispatch("recipel/updateRecordsInfo", "");
				context.$store.dispatch("recipel/updateBillInfo", "");
				goBackHome();
			}, 500);
		}
	});
}

function makeUrl(url) {
	return '/api' + url
}

function goBackHome() {
	context.$router.push({
		name: "homepage"
	});
}
/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function get(data) {
	return new Promise((resolve, reject) => {
		var _that;
		console.log(data, 'data')
		if ('that' in data.params) {
			_that = data.params.that;
			_that.$store.dispatch("route/updateLoading", true);
			delete data.params.that;
		}
		let url = makeUrl(data.url);
		console.log(context.$store.state, 'this.$store.state')
		axios.get(url, {
			params: data.params.data,
			headers: {
				"content-type": "application/json;charset=UTF-8",
				"Hospital-Id": context.$store.state.terminfo.hospId,
				"Authorization": context.$store.state.user.userinfo.token
			}
		}).then(res => {
			if (_that) {
				_that.$store.dispatch("route/updateLoading", false);
			}
			resolve(res.data);
		}).catch(err => {
			if (_that) {
				_that.$store.dispatch("route/updateLoading", false);
			}
			reject(err.data)
			return checkStatus(err)
		})
	});
}
/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function post(data) {
	return new Promise((resolve, reject) => {
		var _that = data.params.that;
		let url = makeUrl(data.url);
		axios.post(url,
				data.params, {
					headers: {
						"content-type": "application/json;charset=UTF-8",
						"Hospital-Id": context.$store.state.terminfo.hospId,
						"Authorization": context.$store.state.user.userinfo.token
					}
				})
			.then(res => {
				resolve(res.data);
				_that.$store.dispatch("route/updateLoading", false);
			})
			.catch(err => {
				reject(err.data)
				return checkStatus(err)
			})
	});
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function postParams(data) {
	return new Promise((resolve, reject) => {
		context.$store.dispatch("route/updateLoading", true);
		let url = makeUrl(data.url);
		axios.post(url,
				data.params, {
					headers: {
						"content-type": "application/json;charset=UTF-8",
						"Hospital-Id": context.$store.state.terminfo.hospId,
						"Authorization": context.$store.state.user.userinfo.token
					}
				})
			.then(res => {
				resolve(res.data);
				_that.$store.dispatch("route/updateLoading", false);
			})
			.catch(err => {
				reject(err.data)
				return checkStatus(err)
			})
	});
}




/**
 * post方法，对应post请求 json格式
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function jsonPost(data) {
	return new Promise((resolve, reject) => {
		var _that;
		let url = makeUrl(data.url);
		if ('that' in data.params) {
			_that = data.params.that;
			_that.$store.dispatch("route/updateLoading", true);
			delete data.params.that;
		}
		axios.post(url, JSON.stringify(data.params.data), {
				headers: {
					"content-type": "application/json;charset=UTF-8",
					"Hospital-Id": context.$store.state.terminfo.hospId,
					"Authorization": context.$store.state.user.userinfo.token
				}
			})
			.then(res => {
				if (_that) {
					_that.$store.dispatch("route/updateLoading", false);
				}
				resolve(res.data);
			})
			.catch(err => {
				if (_that) {
					_that.$store.dispatch("route/updateLoading", false);
				}
				reject(err.data)
				return checkStatus(err)
			})
	});
}


/**
 * put方法，对应post请求 json格式
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function jsonPut(data) {
	return new Promise((resolve, reject) => {
		var _that;
		let url = makeUrl(data.url);
		if ('that' in data.params) {
			_that = data.params.that;
			_that.$store.dispatch("route/updateLoading", true);
			delete data.params.that;
		}
		axios.put(url, JSON.stringify(data.params.data), {
				headers: {
					"content-type": "application/json;charset=UTF-8",
					"Hospital-Id": context.$store.state.terminfo.hospId,
					"Authorization": context.$store.state.user.userinfo.token
				}
			})
			.then(res => {
				if (_that) {
					_that.$store.dispatch("route/updateLoading", false);
				}
				resolve(res.data);
			})
			.catch(err => {
				if (_that) {
					_that.$store.dispatch("route/updateLoading", false);
				}
				reject(err.data)
				return checkStatus(err)
			})
	});
}
